package com.wy.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.wy.common.Result;
import com.wy.entity.UserAddress;
import com.wy.service.UserAddressService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author:王渝
 * @Date: 2022/12/13  13:01
 * @Version 1.0
 */
@RequestMapping("/address")
@RestController
@Slf4j
public class UserAddressController {

    @Autowired
    private UserAddressService userAddressService;

    /**
     * 根据用户id查询所有地址
     * @param userId 用户id
     * @return
     */
    @RequestMapping("/getAll/{userId}")
    public Result<List<UserAddress>> getAllAddressByUserId(@PathVariable Long userId){
        LambdaQueryWrapper<UserAddress> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(UserAddress::getUserId,userId);
        //select * from user_address where user_id = ?
        List<UserAddress> list = userAddressService.list(lambdaQueryWrapper);
        for (UserAddress userAddress : list) {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(userAddress.getPerson())
                    .append(" ")
                    .append(userAddress.getPhone())
                    .append(" ")
                    .append(userAddress.getDetailedAddress());
            userAddress.setAll(stringBuilder.toString());
        }
        return Result.success(list);
    }

    /**
     * 添加地址
     * @param userAddress 地址信息
     * @return
     */
    @RequestMapping("/addAddress")
    public Result<String> addAddress(@RequestBody UserAddress userAddress){
        LambdaUpdateWrapper<UserAddress> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        boolean save = userAddressService.save(userAddress);
        if(save){
            return Result.success("添加成功");

        }else {
            return Result.error("添加失败");
        }
    }

    /**
     * 根据地址id修改地址
     * @param userAddress 地址信息
     * @return
     */
    @RequestMapping("/changeLocation")
    public Result<String> changeLocation(@RequestBody UserAddress userAddress){
        LambdaUpdateWrapper<UserAddress> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(UserAddress::getAddressId,userAddress.getAddressId());
        //UPDATE user_address SET person=?, phone=?, detailed_address=?, update_time=? WHERE (address_id = ?)
        boolean update = userAddressService.update(userAddress, lambdaUpdateWrapper);
        if(update){
            return Result.success("更新成功");
        }else {
            return Result.error("更新失败");
        }
    }

    /**
     * 根据地址id删除地址
     * @param addressId 地址id
     * @return
     */
    @RequestMapping("/removeLocation/{addressId}")
    public Result<String> removeLocation(@PathVariable Long addressId){
        //DELETE FROM user_address WHERE address_id=?
        boolean b = userAddressService.removeById(addressId);
        if(b){
            return Result.success("删除成功");
        }else {
            return Result.error("删除失败");
        }
    }

    /**
     * 根据地址id获取地址
     * @param addressId 地址id
     * @return
     */
    @RequestMapping("/getOne/{addressId}")
    public Result<UserAddress> getAddress(@PathVariable Long addressId){
        UserAddress userAddress = userAddressService.getById(addressId);
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(userAddress.getPerson())
                .append(" ")
                .append(userAddress.getPhone())
                .append(" ")
                .append(userAddress.getDetailedAddress());
        userAddress.setAll(stringBuilder.toString());
        return Result.success(userAddress);
    }
}
